



function getNumber(startIdx,content) {
    let len = content.length;
    let i = startIdx;
    let endIdx = -1;
    while (i < len){
        let ch = content[i];
        if (isSpace(ch) || ch == ")"){
            endIdx = i;
            break;
        }
        ++i;
    }
    if (endIdx < 0){
        throw "can not found name!"
    }
    return endIdx;
}

function isSpace(ch){
    return ch == " " || ch == "\t" || ch == "\n" || ch == "\r";
}

function getName(startIdx,content) {
    let len = content.length;
    let i = startIdx;
    let endIdx = -1;
    while (i < len){
        let ch = content[i];
        if (ch === '"'){
            endIdx = i;
            break;
        }
        ++i;
    }
    if (endIdx < 0){
        throw "can not found name!"
    }
    return endIdx;
}

function parseJointLine(str) {
    let idx = str.lastIndexOf("//");
    if (idx >= 0){
        str = str.substring(0,idx).trim();
    }else{
        str = str.trim();
    }

    let arr = [];

    let len = str.length;
    let i = 0;
    while (i < len){
        let ch = str[i];
        if (ch === '"'){
            let endIdx = getName(i + 1,str);
            let name = str.substring(i + 1,endIdx);
            i = endIdx + 1;
            // console.log(`name:${name}`);
            arr.push(name);
        }else if(isSpace(ch) || ch == "(" || ch == ")"){
            i++;
        }else{
            let endIdx = getNumber(i,str);
            let number = str.substring(i,endIdx);
            arr.push(number);
            // console.log(`number:${number}`);
            i = endIdx;
        }
    }

    if (arr.length != 8){
        console.warn(`parse str:${str} error!`);
        return null
    }

    return {
        name:arr[0],
        parentIdx:parseInt(arr[1]),
        x:parseFloat(arr[2]),
        y:parseFloat(arr[3]),
        z:parseFloat(arr[4]),
        rx:parseFloat(arr[5]),
        ry:parseFloat(arr[6]),
        rz:parseFloat(arr[7]),
    }
}

export {parseJointLine}
